package com.atguigu.lease.web.admin.controller.login;


import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SystemUserService systemUserService;

    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        CaptchaVo captcha = loginService.createCaptchaVo();
        return Result.ok(captcha);
    }

    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        String token = loginService.login(loginVo);
        System.out.println("=====================================================");
        System.out.println(token);
        System.out.println("=====================================================");
        return Result.ok(token);

//        // 验证码为空，直接抛出异常
//        if (!StringUtils.hasText(loginVo.getCaptchaCode())){
//            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
//        }
//
//        String key = loginVo.getCaptchaKey();
//        String code = redisTemplate.opsForValue().get(key);
//        if (!StringUtils.hasText(code)){
//            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
//        }
//        if (!loginVo.getCaptchaCode().equals(code)){
//            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
//        }
//
//        LambdaQueryWrapper<SystemUser> systemUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        systemUserLambdaQueryWrapper.eq(SystemUser::getUsername, loginVo.getUsername());
//        SystemUser systemUserFromDB = systemUserService.getOne(systemUserLambdaQueryWrapper);
//        if (systemUserFromDB == null){
//            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
//        }
//        if (systemUserFromDB.getStatus()== BaseStatus.DISABLE){
//            throw new LeaseException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
//        }
//        String yourPwd = DigestUtils.md5Hex(loginVo.getPassword());
//        if (!yourPwd.equals(systemUserFromDB.getPassword())){
//            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
//        }
//
//        String token = JwtUtil.createToken(systemUserFromDB.getId(), systemUserFromDB.getUsername());
//
//        return Result.ok(token);
    }

//    @Operation(summary = "获取登陆用户个人信息")
//    @GetMapping("info")
//    public Result<SystemUserInfoVo> info(HttpServletRequest request) {
//        String token = request.getHeader("access_token");
//        Claims claims = JwtUtil.parseToken(token);
//        Long userId = claims.get("userId", Long.class);
//
//        SystemUser systemUser = systemUserService.getById(userId);
//        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
//        BeanUtils.copyProperties(systemUser, systemUserInfoVo);
//        return Result.ok(systemUserInfoVo);
//    }


    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("info")
    public Result<SystemUserInfoVo> info()  {
        SystemUserInfoVo user = loginService.getSystemUserInfoById(LoginUserContext.getLoginUser().getUserId());
        return Result.ok(user);
    }

}